var express = require('express');
var allSQL = require('./all_sql');
var router = express.Router();

/* GET order listing. */
router.get('/coupon_order/:queryDate?', function(req, res, next) {
    var ds = req.ds;
    var date = req.params.queryDate?new Date(req.params.queryDate):new Date();
    console.log('query coupons at date time',date.toISOString().substr(0,10))
    ds.getConnection(function(err, conn) {
      conn.query( allSQL.COUPON_STATISTICS,date.toISOString().substr(0,10), function(error, results, fields) {
        res.json(results);
        conn.release();
      });
    });
});

router.get('/personal', function(req, res, next) {
    var ds = req.ds;
    var sqlParam = [req.query.city,req.query.from,req.query.to];
    console.log('param======>',sqlParam)
    ds.getConnection(function(err, conn) {
      if (err) {
        console.error('error connecting: ' + err.stack);
        return;
      }else
        console.log('init connection as id ' + conn.threadId);

      conn.query( allSQL.PERSONAL_ORDER,sqlParam,function(error, results, fields) {
        res.json(results);
        conn.release();
      });
    });
});

module.exports = router;
